﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Unity;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.UI;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCP.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;

namespace Baosight.ZNH.ZMCP.UI
{
    /// <summary>
    /// 作业班组管理
    /// </summary>
    /// Title:ZMCP60
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2011-07-05  高强   [创建]
    ///  
    public partial class ZMCP60 : EFViewCmes
    {
        private EIBlock departmentBlock;//三违单位block
        private EIBlock groupLeaderBlock;//检查人block
        public ZMCP60()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 新增/修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            base.Initialize(initData);
            departmentBlock = initData.GetBlock("cmes.cp.department");
            groupLeaderBlock = initData.GetBlock("cmes.cp.groupLeaderName");
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "班组-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }

            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnUpdateButtonClickPopup(eiInfo, "班组-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            AddWorkGroup addWorkGroup = new AddWorkGroup(_dialogType);//构造一个新的修改页面
            addWorkGroup.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock(departmentBlock);//添加三违单位block
            _eIInfo.AddBlock(groupLeaderBlock);//添加班组长block
            addWorkGroup.EIDataContext.Update(_eIInfo);
            if (_dialogType.Equals("update"))
            {
                addWorkGroup.groupName = _eIInfo.GetBlock("result").GetRow(0)["fdGroupName"].ToString();//获得未修改的班组名
            }
            addWorkGroup.fdDepartmentId.IsEnabled = false;
            addWorkGroup.fdDepartmentId.Value = this.EIDataContext.GetAttribute("departmentId");
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = addWorkGroup;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();       
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 选中主表一条记录，显示该条记录的从表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridResult_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (this.grid.SelectedItem != null)
            {
                var _id = this.grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.grid.SelectedItem, null);
                var _departmentId = this.grid.SelectedItem.GetType().GetProperty("fdDepartmentId").GetValue(this.grid.SelectedItem, null);
                var _groupLeaderId = this.grid.SelectedItem.GetType().GetProperty("fdGroupLeaderId").GetValue(this.grid.SelectedItem, null);
                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("fdGroupId", _id.ToString());
                args.Add("fdDepartmentId", _departmentId.ToString());
                args.Add("fdGroupLeaderId", _groupLeaderId.ToString());
                this.DataContent.LoadView<EFTabItem>("ZMCP61", args);
            }
            else
            {
                this.DataContent.ClearValue(EFCustomViewContainerPanel.ContentProperty);//清空报表区域内容
            }
        }

        /// <summary>
        /// 重新基类的发布数据方法
        /// 刷新树节点
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(Platform.Core.EIInfo data)
        {
            if (null != this.grid.SelectedItem)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCP60");
                eiInfo.Set("method", "updateNum");
                string fdKeyId = this.grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.grid.SelectedItem, null) as string;
                eiInfo.Set("fdKeyId", fdKeyId);

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, OnDoUpdateCompleted);
            }
        }

        private void OnDoUpdateCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                base.OpenEFWindowAlert(e.EIInfo.DetailMsg, true);
                return;
            }
            else
            {
                //this.OnQueryButtonClick(null,null);
                EIRow row = (this.grid.SelectedItem as UIBaseEntity).GetEIRow();
                row["fdMemberNum"] = e.EIInfo.GetBlock(EIConstants.ResultBlock).Get(0, "fdMemberNum");
            }
        }
    }
}
